﻿//https://leetcode.cn/problems/maximum-product-subarray/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int maxProduct(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> f(n + 1);
        vector<int> g(n + 1);

        f[0] = g[0] = 1;

        for (int i = 1; i <= n; i++)
        {
            if (nums[i - 1] > 0)
            {
                //记录此时的正最大值和负最小值
                f[i] = max(nums[i - 1], nums[i - 1] * f[i - 1]);
                g[i] = min(nums[i - 1], nums[i - 1] * g[i - 1]);
            }
            else
            {
                f[i] = max(nums[i - 1], nums[i - 1] * g[i - 1]);
                g[i] = min(nums[i - 1], nums[i - 1] * f[i - 1]);
            }
        }

        int ret = INT_MIN;
        for (int i = 1; i <= n; i++)
        {
            ret = max(ret, f[i]);
        }

        return ret;
    }
};

//https://leetcode.cn/problems/partition-equal-subset-sum/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int n = nums.size();
        int sum = 0;
        for (auto e : nums) sum += e;
        if (sum % 2 == 1) return false;
        int aim = sum / 2;

        vector<bool> dp(aim + 1);
        dp[0] = true;

        for (int i = 1; i <= n; i++)
            for (int j = aim; j >= nums[i - 1]; j--)
                dp[j] = dp[j] || dp[j - nums[i - 1]];

        return  dp[aim];

    }
};
